package calculadora;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import exceptions.OperandException;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class CalcGui extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField resultField;
	private JButton jButtonAND;
	private JButton jButtonNOR;
	private JButton jButtonEnter;
	private JButton jButtonMemoryClear;
	private JButton jButtonMemoryRecord;
	private JButton jButtonMemory;
	private JButton jButtonFalse;
	private JButton jButtonTrue;
	private JButton jButtonNOT;
	private JButton jButtonTHEN;
	private JButton jButtonNXOR;
	private JButton jButtonNAND;
	private JButton jButtonXOR;
	private JButton jButtonOR;
	private JButton Clear;
	private LogiCalculator calc; 
	/**
	 * Auto-generated main method to display this JFrame
	 */

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CalcGui inst = new CalcGui();

				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public CalcGui() {
		super();
		initGUI();
		calc = new LogiCalculator();
	}
	/*
	 * listeners
	 */
	public class ClearButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {
				resultField.setText("");
				calc.sendOperation(OPERATION.CLEAR);

			} catch (Exception e) {
				resultField.setText(e.getMessage());
			}

		}
	}
	public class MemoryRecordButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {

			calc.setMemory(calc.isP());
			calc.setMemoryTrash(false);

		}
	}
	public class MemoryButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			try{

				calc.sendOperation(OPERATION.MEMORY);
				resultField.setText(calc.getMemory()+"");
				
			}catch (OperandException e){
				resultField.setText(e.getMessage());
				
			}

		}
	}
	public class MemoryClearButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {

			calc.setMemoryTrash(true);

		}
	}
	public class XORButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {
				calc.sendOperation(OPERATION.XOR);

			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}


		}
	}
	public class NANDButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {

				calc.sendOperation(OPERATION.NAND);



			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}


		}
	}
	public class ANDButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {

				calc.sendOperation(OPERATION.AND);



			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}


		}
	}
	public class ORButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {

			try {
				calc.sendOperation(OPERATION.OR);

			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}

		}
	}
	public class NORButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {

			try {
				calc.sendOperation(OPERATION.NOR);

			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}

		}
	}
	public class NXORButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {

			try {
				calc.sendOperation(OPERATION.NXOR);

			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}

		}
	}
	public class THENButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {
				calc.sendOperation(OPERATION.THEN);

			} catch (OperandException e) {
				resultField.setText(e.getMessage());
			}
		}
	}
	public class TRUEButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {


			if (calc.getState()==STATE.AWAITING_OPERAND){
				calc.setQ(true);
			}else{
				calc.setP(true);
			}
			resultField.setText("TRUE");


		}
	}
	public class FALSEButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			if (calc.getState()==STATE.AWAITING_OPERAND){
				calc.setQ(false);
			}else{
				calc.setP(false);
			}
			resultField.setText("FALSE");

		}
	}
	public class ENTERButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			try {
				
				calc.sendOperation(OPERATION.ENTER);
				if (calc.isGoodResult()){
					resultField.setText(""+calc.getP());
					setState(STATE.AWAITING_OPERAND);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
				
			
			
			

		}
	}
	public class NOTButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent evt) {
			if (!(calc.getState()==STATE.AWAITING_OPERAND)){
			 
				calc.setP(!calc.getP());
			}else{
				try {
				throw new OperandException ("operation error");
				} catch (OperandException e ){
					resultField.setText(e.getMessage());
				}
			}
			resultField.setText(calc.getP()+"");

		}
	}

	/*
	 * init 
	 */
	private void initGUI() {
		try {



			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(thisLayout);
			{
				resultField = new JTextField();
				resultField.setLocale(new java.util.Locale("en", "US"));
			}
			{
				jButtonTrue = new JButton();
				jButtonTrue.setText("TRUE");
				jButtonTrue.addActionListener(new TRUEButtonListener());
			}
			{
				jButtonMemoryRecord = new JButton();
				jButtonMemoryRecord.setText("MR");
				jButtonMemoryRecord.addActionListener(new MemoryRecordButtonListener());
			}
			{
				jButtonMemoryClear = new JButton();
				jButtonMemoryClear.setText("MC");
				jButtonMemoryClear.addActionListener(new MemoryClearButtonListener());
			}
			{
				jButtonEnter = new JButton();
				jButtonEnter.setText("ENTER");
				jButtonEnter.addActionListener(new ENTERButtonListener());
			}
			{
				jButtonMemory = new JButton();
				jButtonMemory.setText("M");
				jButtonMemory.addActionListener(new MemoryButtonListener());
				
			}
			{
				jButtonFalse = new JButton();
				jButtonFalse.setText("FALSE");
				jButtonFalse.addActionListener(new FALSEButtonListener());
				
			}
			{

				Clear = new JButton();
				Clear.setText("Clear");
				Clear.addActionListener(new ClearButtonListener());
			}
			{
				jButtonTHEN = new JButton();
				jButtonTHEN.setText("=>");
				jButtonTHEN.addActionListener(new THENButtonListener());
				
			}
			{
				jButtonNOT = new JButton();
				jButtonNOT.setText("NOT");
				jButtonNOT.addActionListener(new NOTButtonListener());
			}
			{
				jButtonXOR = new JButton();
				jButtonXOR.setText("XOR");
				jButtonXOR.addActionListener(new XORButtonListener());
			}
			{
				jButtonNAND = new JButton();
				jButtonNAND.setText("NAND");
				jButtonNAND.addActionListener(new NANDButtonListener ());
				
			}
			{
				jButtonNOR = new JButton();
				jButtonNOR.setText("NOR");
				jButtonNOR.addActionListener(new NORButtonListener());
				
			}
			{
				jButtonNXOR = new JButton();
				jButtonNXOR.setText("<=>");
				jButtonNXOR.addActionListener(new NXORButtonListener());
			}
			{
				jButtonAND = new JButton();
				jButtonAND.setText("AND");
				jButtonAND.addActionListener(new ANDButtonListener());
			}
			{
				jButtonOR = new JButton();
				jButtonOR.setText("OR");
				jButtonOR.addActionListener(new ORButtonListener());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(resultField, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(Clear, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButtonTrue, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGroup(thisLayout.createParallelGroup()
							.addGroup(thisLayout.createSequentialGroup()
									.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
											.addComponent(jButtonTHEN, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(jButtonNOT, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
											.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
													.addComponent(jButtonNAND, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
													.addComponent(jButtonAND, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
													.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
															.addGap(10)
															.addComponent(jButtonFalse, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
															.addGap(19)))
															.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
																	.addComponent(jButtonNOR, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																	.addComponent(jButtonOR, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
																	.addGroup(thisLayout.createParallelGroup()
																			.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
																					.addComponent(jButtonMemory, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																					.addGap(6))
																					.addGroup(thisLayout.createSequentialGroup()
																							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
																							.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
																									.addComponent(jButtonNXOR, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																									.addComponent(jButtonXOR, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
																									.addComponent(jButtonMemoryRecord, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																									.addGroup(thisLayout.createParallelGroup()
																											.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
																													.addComponent(jButtonEnter, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																													.addGap(6))
																													.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
																															.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
																															.addComponent(jButtonMemoryClear, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
																															.addContainerGap(17, 17));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap(15, 15)
					.addGroup(thisLayout.createParallelGroup()
							.addComponent(jButtonMemory, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addComponent(jButtonFalse, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addComponent(jButtonTrue, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addComponent(jButtonMemoryRecord, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addComponent(jButtonMemoryClear, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
							.addGap(34)
							.addGroup(thisLayout.createParallelGroup()
									.addComponent(resultField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
									.addGroup(thisLayout.createSequentialGroup()
											.addGap(81)
											.addGroup(thisLayout.createParallelGroup()
													.addComponent(jButtonXOR, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
													.addComponent(jButtonOR, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
													.addComponent(jButtonAND, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
													.addComponent(jButtonNOT, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
													.addGap(16)
													.addGroup(thisLayout.createParallelGroup()
															.addComponent(jButtonNXOR, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
															.addComponent(jButtonNOR, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
															.addComponent(jButtonNAND, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
															.addComponent(jButtonTHEN, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
															.addComponent(Clear, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
															.addComponent(jButtonEnter, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))))
															.addContainerGap(21, 21));
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}






}
